package com.apestech.framework.xml.sql.modul;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.Encrypt;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-01-03-11:02
 */
public class SqlRule {
    public class Table{
        private boolean notIn=false;
        private boolean foreignKey=true;
        private String fields;
        private JSONObject param;
        private JSONObject field;
        private String wherefield;

        public Table(JSONObject map) throws Exception {
            if(map==null) return;
            if(map.get("notIn")!=null){
                this.notIn=map.getBoolean("notIn");
                map.remove("notIn");
            }
            if(map.get("foreignKey")!=null){
                this.foreignKey=map.getBoolean("foreignKey");
                map.remove("foreignKey");
            };
            if(map.get("fields")!=null){
                this.fields=map.getString("fields").toUpperCase();
                map.remove("fields");
            }
            if(map.get("wherefield")!=null){
                this.wherefield=map.getString("wherefield");
                map.remove("wherefield");
            }
            if(map.size()>0) param=new JSONObject(map);
        }

        public void setParam(JSONObject param) {
            this.param = param;
        }

        public boolean isNotIn() {
            return notIn;
        }

        public boolean isForeignKey() {
            return foreignKey;
        }

        public String getFields() {
            return fields;
        }

        public JSONObject getParam() {
            return param;
        }

        public void addField(String key, Object value){
            if(field==null) field=new JSONObject();
            field.put(key, value);
        }

        public JSONObject getField() {
            return field;
        }

        public String getWherefield() {
            return wherefield;
        }
    }

    private ArrayList<Table> tables=new ArrayList<Table>();
    private JSONObject table=new JSONObject();
    private String cacheKey;

    public void addTable(String tabname, JSONObject map) throws Exception {
        table.put(tabname, tables.size());
        tables.add(new Table(map));
    }

    public Table getTable(String tabname) throws Exception {
        if(table.get(tabname)==null) return null;
        return tables.get(table.getInteger(tabname));
    }

    public String getCacheKey(String sqlType) {
        if(cacheKey==null){
            StringBuffer s=new StringBuffer();
            int len=tables.size();
            for(int i=0; i<len; i++){
                if(!"".equals(s.toString())) s.append(",");
                s.append(tables.get(i).getFields());
                if(!tables.get(i).isNotIn()) s.append(",_IN");
                if(tables.get(i).isForeignKey()) s.append(",_FK");
            }
            cacheKey=Encrypt.e(table.getFirstKey()+"_"+sqlType+"_"+s.toString());
        }
        return cacheKey;
    }

    public void setParam(String tabname, JSONObject param) throws Exception {
        Table tab=getTable(tabname);
        if(tab!=null){
            tab.setParam(param);
            tables.set(table.getInteger(tabname), tab);
        }
    }

    public void addField(String tabname, String field, JSONObject param) throws Exception {
        Table tab=getTable(tabname);
        if(tab!=null){
            tab.addField(field, param);
            tables.set(table.getInteger(tabname), tab);
        }
    }

    public String getTable() {
        return table.getFirstKey();
    }

    public ArrayList<Table> getTables() {
        return tables;
    }

}
